package com.whfc.iot.util;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.util.Calendar;
import java.util.Date;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/9/7 10:20
 */
public class MsgDataCodecUtil {

    public static final int START_YEAR = 2000;

    /**
     * 读取时间
     *
     * @param buf
     * @return
     */
    public static Date readDateTime(ByteBuf buf) {
        byte[] times = new byte[6];
        buf.readBytes(times);
        int year = START_YEAR + times[0];
        int month = times[1];
        int day = times[2];
        int hour = times[3];
        int minute = times[4];
        int second = times[5];

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 写入时间
     *
     * @param buf
     * @param date
     */
    public static void writeDateTime(ByteBuf buf, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR) - START_YEAR;
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        buf.writeByte(year);
        buf.writeByte(month);
        buf.writeByte(day);
        buf.writeByte(hour);
        buf.writeByte(minute);
        buf.writeByte(second);
    }

    /**
     * 读取时间
     *
     * @param buf
     * @return
     */
    public static Date readDateTimeWithWeek(ByteBuf buf) {
        byte[] times = new byte[7];
        buf.readBytes(times);
        int year = START_YEAR + times[0];
        int month = times[1];
        int day = times[2];
        int week = times[3];
        int hour = times[4];
        int minute = times[5];
        int second = times[6];

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 写入时间
     *
     * @param buf
     * @param date
     */
    public static void writeDateTimeWithWeek(ByteBuf buf, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR) - START_YEAR;
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);

        switch (week) {
            case Calendar.SUNDAY:
                week = 7;
            default:
                week = week - 1;
        }

        buf.writeByte(year);
        buf.writeByte(month);
        buf.writeByte(day);
        buf.writeByte(week);
        buf.writeByte(hour);
        buf.writeByte(minute);
        buf.writeByte(second);
    }

    /**
     * 读取指定长度byte 转成字符串
     * @param buf
     * @param i
     * @return
     */
    public static String readToString(ByteBuf buf, int i) {
        byte[] bytes = new byte[i];
        buf.readBytes(bytes);
        return new String(bytes);
    }

    public static void main(String[] args) {
        ByteBuf buf = Unpooled.buffer(5);
        buf.writeByte(0x35);
        buf.writeByte(0x30);
        buf.writeByte(0x31);
        buf.writeByte(0x33);
        buf.writeByte(0x41);
        System.out.println(readToString(buf,5));
    }
}
